import ScrollBar from "./ScrollBar";
import DateTool from "./DateTool";
import CacheTool from "./CacheTool";

interface MsgData {
    time: number,
    content: string,
    avatar: string,
    name: string,
    username:string
}

/**
 *  消息处理工具
 * @author 王强
 * @phone 13052079525
 */
class MessageTool {
    static time: number;
    /**
     * @param object info 
     */
    static showMsg(info: MsgData, content: string): string {// 展示收到的消息
        // 清除系统消息
        console.log(info);
        // $('.laykefu-chat-system').html('');
        MessageTool.clearMsg();//清除已有提醒

        return MessageTool.msgFactory(content, 'mine', info )
    
    }

    /**
     *  $(".laykefu-img").click(function () {
            $(".laykefu-bigimg").css({
                height: $(this).height() * 5,
                width: $(this).width() * 5
            });
            $(".laykefu-mask").fadeIn();
            $(".laykefu-bigimg").attr("src", $(this).attr("src")).fadeIn()
        });
     */
    static showBigPic() {// 双击图片
       
    }

    static newMsg() {//消息提醒
        var title = document.title.replace("【　　　】", "").replace("【新消息】", "");
        // 定时器，设置消息切换频率闪烁效果就此产生  

       let  timer = setTimeout(function () {
            MessageTool.time++;
            MessageTool.newMsg();
            if (  MessageTool.time % 2 == 0) {
                document.title = "【新消息】" + title
            }
            else {
                document.title = "【　　　】" + title
            };
        }, 600);
        return timer;
    }

    static clearMsg() {// 清除消息提示
        // clearTimeout(timer);
        document.title = document.title.replace("【　　　】", "").replace("【新消息】", "");
    }

    static replaceContent (content: string) {// 转义聊天内容中的特殊字符
        // 支持的html标签
        var html = function (end: string) {
            return new RegExp('\\n*\\[' + (end || '') + '(pre|div|span|p|table|thead|th|tbody|tr|td|ul|li|ol|li|dl|dt|dd|h2|h3|h4|h5)([\\s\\S]*?)\\]\\n*', 'g');
        };
        content = (content || '').replace(/&(?!#?[a-zA-Z0-9]+;)/g, '&amp;')
            .replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/'/g, '&#39;').replace(/"/g, '&quot;') // XSS
            .replace(/face\[([^\s\[\]]+?)\]/g, function (face) {  // 转义表情
                var alt = face.replace(/^face/g, '');
                return '<img alt="' + alt + '" title="' + alt + '" src="' + getFaces()[alt] + '">';
            })
            .replace(/img\[([^\s]+?)\]/g, function (img) {  // 转义图片
                return '<img class="laykefu-img" src="' + img.replace(/(^img\[)|(\]$)/g, '') + '" width="100px" height="100%">';
            })
            .replace(/file\([\s\S]+?\)\[[\s\S]*?\]/g, function (str) { // 转义文件
                var href = (str.match(/file\(([\s\S]+?)\)\[/) || [])[1];
                var text = (str.match(/\)\[([\s\S]*?)\]/) || [])[1];
                if (!href) return str;
                return '<a class="laykefu-file" href="' + href + '" download target="_blank"><i class="layui-icon">&#xe61e;</i><cite>' + (text || href) + '</cite></a>';
            }).replace(html(''), '\<$1 $2\>').replace(html('/'), '\</$1\>') // 转移HTML代码
            .replace(/\n/g, '<br>') // 转义换行

        return content;
    }

    static msgFactory (content:string,  type:string, uinfo:MsgData) {// 消息体    
        var _html = '';
        if ('mine' == type) {
            _html += '<li class="laykefu-chat-mine">';
        } else {
            _html += '<li>';
        }
        _html += '<div class="laykefu-chat-user">';
        _html += '<img src="' + uinfo.avatar + '">';
        if ('mine' == type) {
            _html += '<cite><i>' + DateTool. getDate() + '</i>' + uinfo.username + '</cite>';
        } else {
            _html += '<cite>' + uinfo.name + '<i>' + DateTool.getDate() + '</i></cite>';
        }
        _html += '</div><div class="laykefu-chat-text">' + MessageTool. replaceContent(content) + '</div>';
        _html += '</li>';
    
        return _html;
    }

    static showSystem ({content}: {[content:string]:string}) {
        var _html = '<div class="laykefu-chat-system"><span>' + content + '</span></div>';
        // $("#laykefu-chat-list ul").append(_html);
    }
    static notice(state: boolean) {//新消息红点提示
        // state ? $('.laykefu-min').append('<i class="laykefu-newMsg"></i>') : $('.laykefu-newMsg').remove();
    }
}

export default MessageTool;